package com.yanfei.zero.apis;

import java.io.IOException;
import java.util.stream.Stream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yanfei.zero.application.handler.secret.IgnoreDecrypt;
import com.yanfei.zero.domain.user.model.User1;
import com.yanfei.zero.domain.user.repository.User1Repository;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.transaction.Transactional;
import lombok.extern.slf4j.Slf4j;
/**
 * cpu 层面的优化 （主要针对循环内的操作）  1 保证顺序 2 拆分循环  3 利用CPU的局部性原理（CPU缓存 寄存器） 4 尽量批量
 */
@RestController
@Tag(name="代码优化")
@Slf4j
public class CpuCodeOptimizeController {
	
	@Autowired
	private User1Repository user1Repository;
	/**
	 * @return
	 * @throws IOException 
	 */
	@Operation(summary = "for 循环优化")
    @GetMapping(value="/forOptimize",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
	// @AddLock("lock:121")
//    @AddLog
	@IgnoreDecrypt
	@Transactional
	@CrossOrigin(originPatterns = "*", methods = {RequestMethod.GET, RequestMethod.POST})
	Object  Optimize1(String id) throws IOException {
		processAll();
		return true;
    }
	
	// 判断一个Java Stream 操作是惰性求值还是及早求值很简单：只需看它的返回值。
	// 如果返回值是Stream，那么是惰性求值,否则则为及早求值。
	public static void main(String[] args) {
        Stream.of(1, 2, 3, 4, 5).filter(num->{
     	    // 打印整个列表  
     	   System.out.println(num);  
            return true;
         })  ;
       
        Stream.of(1, 2, 3, 4, 5).filter(num->{
   	     // 打印整个列表  
   	    System.out.println("stream11"+num);
           return true;
        })
        .count()  ;
    
	}
	
	
	    public void processAll() {
	    	 Long id = 1000L;
	         Pageable pageable = PageRequest.of(0, 100);
	    }
}
